package com.istock.user.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.istock.base.common.api.model.ApiBaseResponse;
import com.istock.base.common.api.model.PageInfo;
import com.istock.base.common.api.model.RetStatusEnum;
import com.istock.base.serializer.JsonSerializer;
import com.istock.base.serializer.json.JsonSerializerFactory;
import com.istock.union.user.model.Constants;
import com.istock.union.user.model.MenuInfo;
import com.istock.union.user.model.OperLog;
import com.istock.union.user.model.RoleInfo;
import com.istock.union.user.model.SecurityChecker;
import com.istock.union.user.model.ext.MenuInfoVO;
import com.istock.union.user.service.MenuInfoService;

@Controller
@RequestMapping("/s/menuInfo")
public class MenuInfoController {

	@Autowired
	private MenuInfoService menuInfoService;
	
	@RequestMapping("showList")
	@SecurityChecker("user:menuInfo:search")
	@OperLog(functionName="查询" , menuName ="资源管理")
	public String showList(ModelMap model) {
//		return "menuInfo/list";
		return "user-app/menuInfo/list2";
	}
	
	@RequestMapping("findButtonByMenuId")
	@SecurityChecker("user:menuInfo:search")
	public @ResponseBody ApiBaseResponse<PageInfo<MenuInfo>> findButtonByMenuId(String id){
		PageInfo<MenuInfo> pageInfo = new PageInfo<MenuInfo>();
		if(StringUtils.isNotBlank(id)) {
			List<MenuInfo> menuList = menuInfoService.findButtonByParent(id);
			pageInfo.setData(menuList);
		}
		return new ApiBaseResponse<PageInfo<MenuInfo>>(RetStatusEnum.SUCCESS,Constants.API_RESPONSE_SUCCESS_CODE,
				Constants.API_RESPONSE_SUCCESS_MESSAGE, pageInfo);
	}
	
	@RequestMapping("addPre")
	@SecurityChecker("user:menuInfo:add")
	public String addPre(ModelMap model,String id){
		/*MenuInfoVO menuInfo = new MenuInfoVO();
		if(StringUtils.isNotBlank(id) && !id.equalsIgnoreCase("0")){
			MenuInfoVO parentMenu = menuInfoService.findMenuInfoById(id);
			menuInfo.setParentId(parentMenu.getId());
			menuInfo.setParentName(parentMenu.getMenuName());
		}else{
			menuInfo.setParentName("最外层");
			menuInfo.setParentId("0");
			
		}
		model.put("edit", menuInfo);*/
		
		List<Map<String , Object>> treeData = menuInfoService.menuTreeSelect(true, null);
		JsonSerializer serializer = JsonSerializerFactory.findSerializer();
		model.put("treeData", serializer.serialize(treeData));
//		model.put("treeData", JSON.toJSONString(treeData));
		
		
		model.put("parentId", id);
		
//		return "menuInfo/add";
		return "user-app/menuInfo/add2";
	}
	
	@RequestMapping("editPre")
	@SecurityChecker("user:menuInfo:edit")
	public String editPre(ModelMap model,String id){
		MenuInfoVO menuInfo = null;
		if(StringUtils.isNotBlank(id)){
			menuInfo = menuInfoService.findMenuInfoById(id);
		}else{
			menuInfo = new MenuInfoVO();
			menuInfo.setMenuName("本系统");
//			menuInfo.setId("0");
			menuInfo.setParentId("0");
			
		}
		model.put("edit", menuInfo);
		
		List<Map<String , Object>> treeData = menuInfoService.menuTreeSelect(true, null);
		JsonSerializer serializer = JsonSerializerFactory.findSerializer();
		model.put("treeData", serializer.serialize(treeData));
//		model.put("treeData", JSON.toJSONString(treeData));
		return "user-app/menuInfo/edit2";
//		return "menuInfo/edit";
	}
	
	@RequestMapping("save")
	@OperLog(functionName="保存" , menuName ="资源管理")
	public @ResponseBody ApiBaseResponse<Boolean> save(ModelMap model , MenuInfo menuInfo){
		try{
			menuInfoService.saveMenuInfo(menuInfo);
			return new ApiBaseResponse<Boolean>(RetStatusEnum.SUCCESS , Constants.API_RESPONSE_SUCCESS_CODE,Constants.API_RESPONSE_SUCCESS_MESSAGE, true);
		}catch(Exception e){
			
		}
		return new ApiBaseResponse<Boolean>(RetStatusEnum.FAIL , Constants.API_RESPONSE_SUCCESS_CODE,Constants.API_RESPONSE_FAIL_MESSAGE, false);
	}
	
	@RequestMapping("checkMenuNameUnique")
	public @ResponseBody ApiBaseResponse<Boolean> checkMenuNameUnique(ModelMap model , String id , String parentId , String menuName){
		if(StringUtils.isBlank(parentId)){
			parentId = "0";
		}
		Long count = menuInfoService.findMenuCount(id, parentId, menuName);
		return new ApiBaseResponse<Boolean>(RetStatusEnum.SUCCESS , Constants.API_RESPONSE_SUCCESS_CODE,Constants.API_RESPONSE_SUCCESS_MESSAGE, count == 0);
	}
	
	@SecurityChecker("user:menuInfo:remove")
	@RequestMapping("/remove")
	@ResponseBody
	@OperLog(functionName="删除",menuName="菜单管理")
	public ApiBaseResponse<Boolean> remove(String id) {
		if (menuInfoService.countMenusByParentId(id) > 0) {
			return new ApiBaseResponse<Boolean>(RetStatusEnum.FAIL,Constants.API_RESPONSE_FAIL_CODE,  "存在子菜单,不允许删除", false);
		}
		if (menuInfoService.countRolesByMenuId(id) > 0) {
			return new ApiBaseResponse<Boolean>(RetStatusEnum.FAIL, Constants.API_RESPONSE_FAIL_CODE, "菜单已分配,不允许删除", false);
		}
		boolean del = menuInfoService.deleteMenuById(id) > 0 ? true : false;
		if (!del) {
			return new ApiBaseResponse<Boolean>(RetStatusEnum.FAIL,Constants.API_RESPONSE_FAIL_CODE,  "菜单已分配,不允许删除", false);
		}
		return new ApiBaseResponse<Boolean>(RetStatusEnum.SUCCESS,Constants.API_RESPONSE_SUCCESS_CODE,  Constants.API_RESPONSE_SUCCESS_MESSAGE, true);
	}
	
	/**
     * 加载所有菜单列表树
     */
	@RequestMapping("/menuTreeData")
    @ResponseBody
    public List<Map<String, Object>> menuTreeData(RoleInfo role)
    {
        List<Map<String, Object>> tree = menuInfoService.menuTreeData(false);
        return tree;
    }
	
	/**
	 * 加载角色菜单列表树
	 */
	@RequestMapping("/roleMenuTreeData")
	@ResponseBody
	public List<Map<String, Object>> roleMenuTreeData(RoleInfo roleInfo) {
		List<Map<String, Object>> tree = menuInfoService.roleMenuTreeData(roleInfo , true);
		return tree;
	}
	
	@RequestMapping("menuDirSelect")
	@ResponseBody
	public List<Map<String , Object>> menuDirSelect(){
		List<Map<String, Object>> tree = menuInfoService.menuTreeData(true);
		return tree;
	}
	
}
